package com.bawei.day18_mediaplayer2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.bawei.day18_mediaplayer2.adapter.SongAdapter;
import com.bawei.day18_mediaplayer2.bean.NetMusicList;
import com.bawei.day18_mediaplayer2.bean.NetMusic;
import com.bawei.day18_mediaplayer2.bean.SongBean;
import com.bawei.day18_mediaplayer2.service.DownloadService;
import com.google.gson.Gson;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class Main2Activity extends AppCompatActivity {
    private static final String TAG = "ytx";
    private String url_list = "https://autumnfish.cn/search?keywords=周杰伦";
    private String url_music = "https://autumnfish.cn/song/url?id=";
    private List<SongBean> list = new ArrayList<>();
    private ListView listView;
    private SongAdapter songAdapter;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            requestPermissions(new String[]{
                    "android.permission.WRITE_EXTERNAL_STORAGE",
                    "android.permission.READ_EXTERNAL_STORAGE"
            },101);
        }
        new MyTask().execute();
        listView = findViewById(R.id.lv);
        //点击列表下载音乐
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(Main2Activity.this, DownloadService.class);
                intent.putExtra("url",list.get(position).getData());
                intent.putExtra("name",list.get(position).getName());
                startService(intent);
            }
        });




    }
    class MyTask extends AsyncTask<String,String,String>{

        @Override
        protected String doInBackground(String... strings) {
            StringBuffer stringBuffer = new StringBuffer();
            try {
                URL url = new URL(url_list);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setConnectTimeout(6000);
                httpURLConnection.setReadTimeout(6000);
                httpURLConnection.connect();
                InputStream inputStream = httpURLConnection.getInputStream();
                byte[] bytes = new byte[1024];
                int len = 0;
                while ((len = inputStream.read(bytes))!=-1){
                    stringBuffer.append(new String(bytes,0,len));
                }
                NetMusicList netMusicList = new Gson().fromJson(stringBuffer.toString(), NetMusicList.class);
                List<NetMusicList.ResultBean.SongsBean> songs = netMusicList.getResult().getSongs();
                for (NetMusicList.ResultBean.SongsBean song : songs) {
                    String name = song.getName();//歌曲名称
                    int id = song.getId();//歌曲id
                    String img1v1Url = song.getArtists().get(0).getImg1v1Url();//封面

                    //根据歌曲id获得歌曲的地址
                    StringBuffer stringBuffer2 = new StringBuffer();
                    URL url2 = new URL(url_music+id);
                    HttpURLConnection httpURLConnection2 = (HttpURLConnection) url2.openConnection();
                    httpURLConnection2.setConnectTimeout(6000);
                    httpURLConnection2.setReadTimeout(6000);
                    httpURLConnection2.connect();
                    InputStream inputStream2 = httpURLConnection2.getInputStream();
                    byte[] bytes2 = new byte[1024];
                    int len2 = 0;
                    while ((len2 = inputStream2.read(bytes2))!=-1){
                        stringBuffer2.append(new String(bytes2,0,len2));
                    }
                    NetMusic netMusic = new Gson().fromJson(stringBuffer2.toString(), NetMusic.class);
                    String url1 = netMusic.getData().get(0).getUrl();//歌曲地址
                    list.add(new SongBean(name,"周杰伦",url1,img1v1Url));
                }




            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            //打印集合中的数据+展现在listView
            Log.d(TAG, "onPostExecute: "+list);
            songAdapter  = new SongAdapter(list,Main2Activity.this);
            listView.setAdapter(songAdapter);

        }
    }


}
